//
//  KefuTableViewCell.m
//  MyNewProject
//
//  Created by 王春辉 on 1/13/20.
//  Copyright © 2020 sun. All rights reserved.
//

#import "KefuTableViewCell.h"

static CGFloat margin = 15.0f; // icon 距离边框的编剧
static CGFloat icon_width = 40.0f; //

static NSString *const left_img_name = @"kefu_msg_recieve_bg_img";
static NSString *const right_img_name = @"kefu_msg_repeat_bg_img";
static NSString *const icon_img_name = @"auction_customer_service_img";
static NSString *const default_icon_img_name = @"auction_header_score_shoping_btn_img";

@interface KefuTableViewCell()<UITextViewDelegate> {
    UIImageView *_leftBg;
    UIImageView *_rightBg;
}
@property (nonatomic, strong) UILabel *leftLabel;
@property (nonatomic, strong) UILabel *rightLabel;
@property (nonatomic, strong) UIImageView *leftIcon;
@property (nonatomic, strong) UIImageView *rightIcon;

@property (nonatomic, strong) UITextView *leftTextView;

@property (nonatomic, copy) void(^linkSelectBlock)(NSString *);
@end
@implementation KefuTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = SKColorWithHex(0xE1E1E1);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupContentViews];
    }
    return self;
}

- (void)setupContentViews {
    UIImageView *leftBg = [[UIImageView alloc] initWithImage:SKIMAGE_NAMED(left_img_name)];
    [self addSubview:leftBg];
    UIImageView *rightBg = [[UIImageView alloc] initWithImage:SKIMAGE_NAMED(right_img_name)];
    [self addSubview:rightBg];
    
    _leftBg = leftBg;
    _rightBg = rightBg;
    _leftBg.hidden = true;
    _rightBg.hidden = true;
    
    [self.leftIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_offset(margin);
        make.size.mas_equalTo(CGSizeMake(icon_width, icon_width));
        make.bottom.mas_lessThanOrEqualTo(-margin);
    }];
    [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.leftIcon.mas_right).mas_offset(margin + 10);
        make.right.mas_lessThanOrEqualTo(self.rightIcon.mas_left).mas_offset(-margin);
        make.top.mas_equalTo(self.leftIcon).mas_offset(margin);
        make.bottom.mas_lessThanOrEqualTo(-margin);
    }];
    [leftBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.leftLabel).mas_offset(-20);
        make.right.mas_equalTo(self.leftLabel).mas_offset(10);
        make.top.mas_equalTo(self.leftLabel).mas_offset(-10);
        make.bottom.mas_equalTo(self.leftLabel).mas_offset(10);
    }];
    
    [self.rightIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-margin);
        make.top.mas_offset(margin);
        make.bottom.mas_lessThanOrEqualTo(-margin);
        make.size.mas_offset(CGSizeMake(icon_width, icon_width));
    }];
    [self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.rightIcon.mas_left).mas_offset(-margin - 10);
        make.top.mas_equalTo(self.rightIcon).mas_offset(margin);
        make.bottom.mas_lessThanOrEqualTo(-margin);
    }];
    [rightBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.rightLabel).mas_offset(-10);
        make.right.mas_equalTo(self.rightLabel).mas_offset(20);
        make.top.mas_equalTo(self.rightLabel).mas_offset(-10);
        make.bottom.mas_equalTo(self.rightLabel).mas_offset(10);
    }];
    [self.leftTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.leftLabel);
    }];
}


#pragma mark -- mark 懒加载
- (UILabel *)leftLabel {
    if (!_leftLabel) {
        _leftLabel = [[UILabel alloc] init];
        _leftLabel.textAlignment = NSTextAlignmentLeft;
        _leftLabel.font = SKFONT_SIZE(13);
        _leftLabel.textColor = SKColorWithHex(0x000000);
        _leftLabel.hidden = true;
        _leftLabel.numberOfLines = 0;
        _leftLabel.text = @" ";
        [self addSubview:_leftLabel];
    }
    return _leftLabel;
}
- (UITextView *)leftTextView {
    if (!_leftTextView) {
        _leftTextView = [[UITextView alloc] initWithFrame:CGRectZero];
        _leftTextView.editable = false;
        _leftTextView.textColor = SKColorWithHex(0x000000);
        _leftTextView.font = SKFONT_SIZE(13);
        _leftTextView.delegate = self;
        _leftTextView.text = @"";
        _leftTextView.scrollEnabled = false;
        [self addSubview:_leftTextView];
    }
    return _leftTextView;
}
- (UILabel *)rightLabel {
    if (!_rightLabel) {
        _rightLabel = [[UILabel alloc] init];
        _rightLabel.textAlignment = NSTextAlignmentLeft;
        _rightLabel.font = SKFONT_SIZE(13);
        _rightLabel.textColor = SKColorWithHex(0x000000);
        _rightLabel.hidden = true;
        _rightLabel.numberOfLines = 0;
        _rightLabel.text = @" ";
        [self addSubview:_rightLabel];
    }
    return _rightLabel;
}
- (UIImageView *)leftIcon {
    if (!_leftIcon) {
        _leftIcon = [[UIImageView alloc] initWithImage:SKIMAGE_NAMED(icon_img_name)];
        _leftIcon.layer.masksToBounds = true;
        _leftIcon.layer.cornerRadius = icon_width * 0.5;
        _leftIcon.hidden = true;
        [self addSubview:_leftIcon];
    }
    return _leftIcon;
}
- (UIImageView *)rightIcon {
    if (!_rightIcon) {
        _rightIcon = [[UIImageView alloc] initWithImage:SKIMAGE_NAMED(default_icon_img_name)];
        _rightIcon.layer.masksToBounds = true;
        _rightIcon.layer.cornerRadius = icon_width * 0.5;
        _rightIcon.hidden = true;
        [self addSubview:_rightIcon];
    }
    return _rightIcon;
}


/// 刷新数据
/// @param model model
- (void)refreshWithModel:(KefuContentViewModel *)model LinkSelected:(nonnull void (^)(NSString * _Nonnull))selectedBlock {
    self.linkSelectBlock = selectedBlock;
    if (model.msgType == 0) {
        // 问题
        [self showLeftWithModel:model];
    }else if (model.msgType == 1) {
        // 提问
        [self showRightWithModel:model];
    }else if (model.msgType == 2) {
        // 回答
        [self showLeftAnswerWithModel:model];
    }else { }
}

- (void)showLeftWithModel:(KefuContentViewModel *)model {
    self.leftIcon.hidden = false;
    _leftBg.hidden = false;
    self.rightIcon.hidden = true;
    _rightBg.hidden = true;
    self.leftTextView.hidden = false;
    self.rightLabel.hidden = true;
    self.leftLabel.hidden = true;
    
    self.leftLabel.attributedText = model.attributedString;
    self.leftTextView.attributedText = model.attributedString;
    self.rightLabel.text = @"";
}
- (void)showLeftAnswerWithModel:(KefuContentViewModel *)model {
    self.leftIcon.hidden = false;
    _leftBg.hidden = false;
    self.rightIcon.hidden = true;
    _rightBg.hidden = true;
    self.leftTextView.hidden = false;
    self.rightLabel.hidden = true;
    self.leftTextView.hidden = true;
    self.leftLabel.hidden = false;
    
    self.leftLabel.text = model.content;
    self.leftTextView.text = model.content;
    self.rightLabel.text = @"";
}
- (void)showRightWithModel:(KefuContentViewModel *)model {
    self.leftIcon.hidden = true;
    _leftBg.hidden = true;
    self.rightIcon.hidden = false;
    _rightBg.hidden = false;
    self.leftTextView.hidden = true;
    self.rightLabel.hidden = false;
    
    self.rightLabel.text = model.content;
    self.leftLabel.attributedText = nil;
    self.leftTextView.attributedText = nil;
}


#pragma MARK -- MARK UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    if ([URL.scheme isEqualToString:@"ypyx"]) {
        if (self.linkSelectBlock) {
            NSString *s = [URL.absoluteString stringByReplacingOccurrencesOfString:@"ypyx://" withString:@""];
            NSString *string = [s stringByRemovingPercentEncoding];
            self.linkSelectBlock(string);
            return false;
        }
    }
    return true;
}
@end

#pragma mark -- mark 客服信息内容contentView数据模型实现
@implementation KefuContentViewModel
@synthesize attributedString = _attributedString;

- (NSAttributedString *)attributedString {
    NSString *title = @"亲，猜您想咨询以下问题？可以回复相应数字，只能客服来帮您解答；您也可以发送问题编号，比如：“1”";
    NSString *content = [NSString stringWithFormat:@"%@\n%@", title, self.content];
    NSMutableAttributedString *mAttributeS = [[NSMutableAttributedString alloc] initWithString:content];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineSpacing = 8;
    [mAttributeS addAttributes:@{NSForegroundColorAttributeName: SKColorWithHex(0x3B7CBB), NSUnderlineColorAttributeName : SKColorWithHex(0x3B7CBB), NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle], NSParagraphStyleAttributeName:style} range:[content rangeOfString:self.content]];
    NSArray *sArr = [self.content componentsSeparatedByString:@"\n"];
    for (NSString *s in sArr) {
        NSRange range = [content rangeOfString:s];
        NSString *codingString = [s stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        NSDictionary *attribute = @{NSLinkAttributeName:[NSString stringWithFormat:@"ypyx://%@", codingString]};
        [mAttributeS addAttributes:attribute range:range];
    }
    _attributedString = mAttributeS;
    return _attributedString;
}
@end
